package ourGame;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Graphics;
import javax.swing.*;

public class Board extends JPanel implements ActionListener
{
	Ship s;
	Image img;
	Timer time;
	Map map;
	
	public Board()
	{
		System.out.println("Creating board.");
		map = new Map();
		s = new Ship();
		addKeyListener(new AL());
		setFocusable(true);
		ImageIcon i = new ImageIcon("images/background.gif");
		img = i.getImage();
		time = new Timer(5, this);
		time.start();
	}
	
	public void actionPerformed(ActionEvent e)
	{
		s.move();
		repaint();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		
		
		g2d.drawImage(img, 0, 0, null);
		map.drawMap(g2d);
		g2d.drawImage(s.getImage(), s.getX(), s.getY(), null);
	}
	
	private class AL extends KeyAdapter
	{
		public void keyPressed(KeyEvent e)
		{
			switch(e.getKeyCode())
			{
				case KeyEvent.VK_LEFT:
					s.setVelocity(-1, 0);
					break;
				case KeyEvent.VK_RIGHT:
					s.setVelocity(1, 0);
					break;
				case KeyEvent.VK_UP:
					s.setVelocity(0, -1);
					break;
				case KeyEvent.VK_DOWN:
					s.setVelocity(0, 1);
					break;
				case KeyEvent.VK_S:
					s.setVelocity(0, 0);
					break;
				case KeyEvent.VK_F:
					s.setVelocity(9, -9);
					break;
			}
		}
		public void keyReleased(KeyEvent e)
		{
			/*
			switch(e.getKeyCode())
			{
				case KeyEvent.VK_LEFT:
					s.setVelocity(0, 0);
					break;
				case KeyEvent.VK_RIGHT:
					s.setVelocity(0, 0);
					break;
				case KeyEvent.VK_UP:
					s.setVelocity(0, 0);
					break;
				case KeyEvent.VK_DOWN:
					s.setVelocity(0, 0);
					break;
			}
			*/
		}
	}
}
